 //
//  OAuthViewController.m
//  王雷Sinoweibo
//
//  Created by wanglei on 16/10/30.
//  Copyright © 2016年 wanglei. All rights reserved.
//

#import "OAuthViewController.h"
#import "SVProgressHUD/SVProgressHUD.h"
#import "AFNetworking.h"
#import "UserAccount.h"
#import "AccountViewModel.h"
#import "WelcomeViewController.h"
#import "MainViewController.h"

typedef void (^success)(BOOL isSuccess);
@interface OAuthViewController()<UIWebViewDelegate>

@end

@implementation OAuthViewController

-(void)loadView{
    UIWebView *Webview = [[UIWebView alloc]init];
    _webview = Webview;
    self.view = _webview;
    _webview.delegate = self;
}

- (void)close{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}



- (void)AccountFortest{
    
    NSString *str = @"document.getElementById('userId').value = '1039448826@qq.com',document.getElementById('passwd').value = 'wang814'";
    
    [self.webview stringByEvaluatingJavaScriptFromString:str];
}

-(void)viewDidLoad{
    [super viewDidLoad];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(close)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"测试" style:UIBarButtonItemStylePlain target:self action:@selector(AccountFortest)];
        [self loadOauthPage];
}

- (void)loadOauthPage{
    //    NSString *urlstring = @"https://api.weibo.com/oauth2/authorize?client_id=1035928713&redirect_uri=https://www.baidu.com";
    
    NSString *urlstring = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",client_id,redirect_uri];
    
    NSURL *url = [NSURL URLWithString:urlstring];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [self.webview loadRequest:request];
    
}


//webview的代理方法的实现
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;{
    
    //    NSLog(@"%@",request.URL);
    NSString *urlstr = [request.URL absoluteString];
    
    NSLog(@"---%@-----",urlstr);
    
    if (urlstr != nil) {
        
        if ([urlstr containsString:@"code"]) {
            
            
            if (request.URL.query != nil) {
                
                //   https://m.baidu.com/?code=788337cbdf165c21f12eb37b8cdb4b97&from=844b&vit=fps
                            
                NSLog(@"%@",request.URL.query);
                //  code=761de8280047af4ceb024f975a1decbf&from=844b&vit=fps
                
                NSRange range = NSMakeRange(5, 32);
                
                NSString *code = [request.URL.query substringWithRange:range];
                
                NSLog(@"%@",code);
                
                AccountViewModel *accountModel = [AccountViewModel sharedAccountModel];
                
                NSLog(@"%@",accountModel);
                
                [accountModel loadAccessToken:code isSuccess:^(BOOL isSuccess) {
                    
                    if(isSuccess){
                        
                        [self dismissViewControllerAnimated:YES completion:nil];
                        
//                        WelcomeViewController *welcomecontroller = [[WelcomeViewController alloc]init];
//                        
//                        [UIApplication sharedApplication].keyWindow.rootViewController = welcomecontroller;
                        
                        [[NSNotificationCenter defaultCenter] postNotificationName:KChoseRootVC object:nil];
                        NSLog(@"come here oauview");
                        
                        NSLog(@"数据请求成功了,取到数据了");
                        
                    }else
                    {
                        [SVProgressHUD showErrorWithStatus:@"登录失败,请重新检查"];
                    }
                    
                    
                }];
                
                return false;
                
            }
            return false;
            
        }
//     ??????
        return true;
        
        
    }
    return false;
    
}





- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    
    [SVProgressHUD dismiss];
    
    
}

- (void)webViewDidStartLoad:(UIWebView *)webView;{
    
    [SVProgressHUD show];
    
}



- (void)webViewDidFinishLoad:(UIWebView *)webView;{
    
    [SVProgressHUD dismiss];
}


@end
